package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;
import java.util.List;
import java.util.Random;

public class Mob extends AbstractMapleObject{
    public static Image[] imgs = new Image[100];
    static {
        for (int i = 0; i < 3; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_stand"+ i);
        }
        for (int i = 3; i < 5; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_walk"+ (i - 3));
        }
        for (int i = 5; i < 9; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_die"+ (i - 5));
        }
        imgs[9] = ImageUtil.getImage("blood");
        for (int i = 10; i <13 ; i++) {
            imgs[i] = ImageUtil.getImage("mob01_right_stand"+ (i-10));
        }
        for (int i = 13; i < 15; i++) {
            imgs[i] = ImageUtil.getImage("mob01_right_walk"+ (i - 13));
        }
        for (int i = 15; i < 19; i++) {
            imgs[i] = ImageUtil.getImage("mob01_left_die"+ (i - 15));
        }
        imgs[19] = ImageUtil.getImage("lefthit");
        imgs[20] = ImageUtil.getImage("righthit");


    }

    /**
     * 最大血量
     */

    public int MAX_HP;
    public Mob(){
        this.width = imgs[0].getWidth(null);
        this.height = imgs[0].getHeight(null);
        this.dir = Direction.RIGHT;
        this.action = Action.WALK;
        this.live = true;
        this.MAX_HP = 10000;
        this.HP = MAX_HP;


    }
    public Mob(MapleStoryClient msc, int x, int y,int speed){
        this();
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }
    private int count;
    private int step;
    private int dieStep;

    @Override
    public void draw(Graphics g) {
        if (dieStep >= 3){
            this.live = false;
            dieStep = 0;
        }
        if (!live){
            msc.mobs.remove(this);
            return;
        }
        switch (dir){
            case LEFT:
                switch (action){
                    case STAND:
                        if (count++ % 3 == 0){
                            g.drawImage(imgs[step++ % 3],x,y,null);
                        }else{
                            g.drawImage(imgs[step % 3],x,y,null);
                        }
                        break;
                    case WALK:
                        if (count++ % 3 == 0){
                        g.drawImage(imgs[step++ % 2 +3],x,y,null);
                    }else{
                        g.drawImage(imgs[step % 2 + 3],x,y,null);
                    }
                        break;
                    case DIE:if (count++ % 3 == 0){
                        g.drawImage(imgs[dieStep++ % 4 + 6],x,y,null);
                    }else{
                        g.drawImage(imgs[dieStep % 4 + 6],x,y,null);
                    }
                        break;
                    case HIT:if (count++ % 3 == 0){
                        g.drawImage(imgs[19],x,y,null);
                    }
                    break;
                    default:
                        break;
                }
             break;
            case RIGHT:
                switch (action){
                    case STAND:
                        if (count++ % 3 == 0){
                            g.drawImage(imgs[step++ % 3 + 10],x,y,null);
                        }else{
                            g.drawImage(imgs[step % 3 + 10],x,y,null);
                        }
                        break;
                    case WALK:
                        if (count++ % 3 == 0){
                            g.drawImage(imgs[step++ % 2 +13],x,y,null);
                        }else{
                            g.drawImage(imgs[step % 2 + 13],x,y,null);
                        }
                        break;
                    case DIE:if (count++ % 3 == 0){
                        g.drawImage(imgs[dieStep++ % 4 + 15],x,y,null);
                    }else{
                        g.drawImage(imgs[dieStep % 4 + 15],x,y,null);
                    }
                        break;
                    case HIT:if (count++ % 3 == 0){
                        g.drawImage(imgs[20],x,y,null);
                    }
                    default:
                        break;
                }
                break;
            default:
                break;
        }
        if (HP < MAX_HP){
            bloodBar.draw(g);
        }

        move();
        hit();
    }
    private void outOfBound(){
        if (this.x <=570){
            dir = Direction.RIGHT;
        }
        if (this.x >= Constant.GAME_WIDTH - this.width){

            dir = Direction.LEFT;
        }
    }

    @Override
    public void move() {
        outOfBound();
        switch (dir){
            case LEFT:
                switch (action){
                    case WALK:
                        this.x -= speed;
                        break;
                    default:
                        break;
                }
                break;
            case RIGHT:
                switch (action){
                    case WALK:
                        this.x += speed;
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }

    }
    public BloodBar bloodBar = new BloodBar();
    class BloodBar extends AbstractMapleObject{
        private Image  img;
        public BloodBar(){
            this.img = ImageUtil.getImage("blood");
            this.width = img.getWidth(null);
        }

        @Override
        public void draw(Graphics g) {
           /* //血框
            //当内部类访问外部类相同属性名时,使用外部类.this.属性名
            g.drawRect(Mob.this.x,Mob.this.y - 15,Mob.this.width,10);
            //填充色
            Color c =  g.getColor();
            g.setColor(Color.GREEN);
            g.fillRect(Mob.this.x +1,Mob.this.y - 14,Mob.this.width *Mob.this.HP/Mob.this.MAX_HP,9);
            g.setColor(c);*/
            int n = Mob.this.width/this.width  * Mob.this.HP/Mob.this.MAX_HP;
            for (int i= 0; i<n;i++ ) {
                g.drawImage(img, Mob.this.x + (i * this.width), Mob.this.y - 15, null);
            }

        }

    }
    private int mob_hit_count = 0;
    public void hit() {
        mob_hit_count++;
        if (msc.hero.HP >= 0 && this.getRectangle().intersects(msc.hero.getRectangle())
                && mob_hit_count % 30 == 0) {
            msc.hero.HP -= 30;
        }
    }


}